package cn.mason.echarts.controller;

import cn.mason.echarts.model.User;
import cn.mason.echarts.service.UserService;
import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.security.token.SSOToken;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class UserController {

    @Autowired
    @Qualifier("UserService")
    private UserService userService;

    @RequestMapping(value = "/myinfo",method = RequestMethod.GET)
    public String myinfo() {
        return "myinfo";
    }

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String user(HttpServletRequest request,Model model) {
        List<User> list = new ArrayList<>();
        list = userService.listAll();
        model.addAttribute("userList",list);
        return "user";
    }

    @RequestMapping(value = "/rmUser/{id}",method = RequestMethod.GET)
    public String rmUser(@PathVariable("id") String id, HttpServletRequest request) {
        userService.delete(id);
        return "redirect:/user";
    }

    @RequestMapping(value = "/regPage",method = RequestMethod.GET)
    public String registerPage() {
        return "register";
    }

    @RequestMapping(value = "/loginPage")
    public String loginPage(HttpServletRequest request){
        if(SSOHelper.getSSOToken(request)!=null){
            return "index";
        }
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String login(HttpServletRequest request, HttpServletResponse response, Model model) {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        User u = userService.findByAccount(account);
        if (account == null || password == null || "".equals(account) || "".equals(password)||u!=null) {
            model.addAttribute("register","0");
            String result = JSONObject.fromObject(model).toString();
            return result;
        }

        User userInfo = new User();
        userInfo.setAccount(account);
        userInfo.setPassword(password);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String regTime = simpleDateFormat.format(new Date());
        userInfo.setRegTime(regTime);
        userInfo.setRole("普通用户");
        userService.insert(userInfo);


        Map params = new HashMap();
        params.put("account", account);
        params.put("password", password);
        Iterator itr = userService.listByParams(params).iterator();
        if (itr.hasNext()) {
            User user=(User)itr.next();
            SSOToken ssoToken=new SSOToken();
            ssoToken.setId(user.getId());
            SSOHelper.setCookie(request,response,ssoToken,true);
            model.addAttribute("register","1");
            String result = JSONObject.fromObject(model).toString();
            return result;
        } else {
            model.addAttribute("register","0");
            String result = JSONObject.fromObject(model).toString();
            return result;
        }

    }
}
